package helloworld

import (
	"fmt"
	"github.com/liangdas/mqant/conf"
	"github.com/liangdas/mqant/log"
	"github.com/liangdas/mqant/module"
	"github.com/liangdas/mqant/module/base"
	"github.com/liangdas/mqant/server"
	"time"
)

var Module = func() module.Module {
	this := new(HellWorld)
	return this
}

type HellWorld struct {
	basemodule.BaseModule
}

func (h *HellWorld) GetType() string {
	//很关键,需要与配置文件中的Module配置对应
	return "HelloWorld"
}
func (h *HellWorld) Version() string {
	//可以在监控时了解代码版本
	return "1.0.0"
}

func (h *HellWorld) OnAppConfigurationLoaded(app module.App)  {
	//当App初始化时调用，这个接口不管这个模块是否在这个进程运行都会调用
	h.BaseModule.OnAppConfigurationLoaded(app)
	log.Info("%v OnAppConfigurationLoaded...", h.GetType())
}

func (h *HellWorld) OnInit(app module.App, settings *conf.ModuleSettings) {
	h.BaseModule.OnInit(h, app, settings,
		server.RegisterInterval(20 * time.Second),
		server.RegisterTTL(30 * time.Second),
		//server.ID("MyHelloWorld"),
	)
	h.GetServer().RegisterGO("/say/hi", h.say)
	log.Info("%v OnInit...", h.GetType())
}

func (h *HellWorld) Run(closeSig chan bool) {
	log.Info("%v Run...", h.GetType())
	log.Info("%v Say hello world...", h.GetType())
	<-closeSig
	log.Info("%v Stop...", h.GetType())
}

func (h *HellWorld) OnDestroy() {
	//一定别忘了继承
	h.BaseModule.OnDestroy()
	log.Info("%v OnDestroy...", h.GetType())
}

func (h *HellWorld)say(name string) (r string,err error) {
	return fmt.Sprintf("hi %v",name), nil
}
